package com.examples.springboot.jpa.resources;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.examples.springboot.jpa.pojos.DepartmentDto;
import com.examples.springboot.jpa.repository.DepartmentRepository;

@Controller
public class DepartmentResource {
  private final DepartmentRepository departmentDao;

  @Autowired
  public DepartmentResource(DepartmentRepository departmentDao) {
    this.departmentDao = departmentDao;
  }

  @Transactional
  public List<DepartmentDto> getAll() {
    return StreamSupport.stream(departmentDao.findAll().spliterator(), false)
        .map(e -> new DepartmentDto(e))
        .collect(Collectors.toList());
  }
}
